Source for file NetPBM.php

Documentation is available at NetPBM.php

  1. <?php
  2. /***********************************************************************
  3. ** Title.........:  NetPBM Driver
  4. ** Version.......:  1.0
  5. ** Author........:  Xiang Wei ZHUO <wei@zhuo.org>
  6. ** Filename......:  NetPBM.php
  7. ** Last changed..:  30 Aug 2003 
  8. ** Notes.........:  Orginal is from PEAR
  9. **/
  10.  
  11. // +----------------------------------------------------------------------+
  12. // | PHP Version 4                                                        |
  13. // +----------------------------------------------------------------------+
  14. // | Copyright (c) 1997-2002 The PHP Group                                |
  15. // +----------------------------------------------------------------------+
  16. // | This source file is subject to version 2.02 of the PHP license,      |
  17. // | that is bundled with this package in the file LICENSE, and is        |
  18. // | available at through the world-wide-web at                           |
  19. // | http://www.php.net/license/2_02.txt.                                 |
  20. // | If you did not receive a copy of the PHP license and are unable to   |
  21. // | obtain it through the world-wide-web, please send a note to          |
  22. // | license@php.net so we can mail you a copy immediately.               |
  23. // +----------------------------------------------------------------------+
  24. // | Authors: Peter Bowyer <peter@mapledesign.co.uk>                      |
  25. // +----------------------------------------------------------------------+
  26. //
  27. // $Id$
  28. //
  29. // Image Transformation interface using command line NetPBM
  30.  
  31. require_once "Transform.php";
  32.  
  33. {
  34.  
  35.     /**
  36.      * associative array commands to be executed
  37.      * @var array 
  38.      */
  39.     var $command = array();
  40.  
  41.     /**
  42.      * Class Constructor
  43.      */
  44.     function Image_Transform_Driver_NetPBM()
  45.     {
  46.         $this->uid md5($_SERVER['REMOTE_ADDR']);
  47.             
  48.         return true;
  49.     // End function Image_NetPBM
  50.  
  51.     /**
  52.      * Load image
  53.      *
  54.      * @param string filename
  55.      *
  56.      * @return mixed none or a PEAR error object on error
  57.      * @see PEAR::isError()
  58.      */
  59.     function load($image)
  60.     {
  61.         //echo $image;
  62.         $this->image $image;
  63.         $this->_get_image_details($image);
  64.     // End load
  65.  
  66.     /**
  67.      * Resizes the image
  68.      *
  69.      * @return none 
  70.      * @see PEAR::isError()
  71.      */
  72.     function _resize($new_x$new_y)
  73.     {
  74.         // there's no technical reason why resize can't be called multiple
  75.         // times...it's just silly to do so
  76.  
  77.         $this->command[IMAGE_TRANSFORM_LIB_PATH .
  78.                            "pnmscale -width $new_x -height $new_y";
  79.  
  80.         $this->_set_new_x($new_x);
  81.         $this->_set_new_y($new_y);
  82.     // End resize
  83.  
  84.     /**
  85.      * Crop the image
  86.      *
  87.      * @param int $crop_x left column of the image
  88.      * @param int $crop_y top row of the image
  89.      * @param int $crop_width new cropped image width
  90.      * @param int $crop_height new cropped image height
  91.      */
  92.     function crop($crop_x$crop_y$crop_width$crop_height
  93.     {
  94.         $this->command[IMAGE_TRANSFORM_LIB_PATH .
  95.                             "pnmcut -left $crop_x -top $crop_y -width $crop_width -height $crop_height";
  96.     }
  97.  
  98.     /**
  99.      * Rotates the image
  100.      *
  101.      * @param int $angle The angle to rotate the image through
  102.      */
  103.     function rotate($angle)
  104.     {
  105.         $angle = -1*floatval($angle);
  106.  
  107.         if($angle 90)
  108.         {   
  109.             $this->command[IMAGE_TRANSFORM_LIB_PATH "pnmrotate -noantialias 90";
  110.             $this->rotate(-1*($angle-90));
  111.         }
  112.         else if ($angle < -90)
  113.         {
  114.             $this->command[IMAGE_TRANSFORM_LIB_PATH "pnmrotate -noantialias -90";
  115.             $this->rotate(-1*($angle+90));
  116.         }
  117.         else
  118.             $this->command[IMAGE_TRANSFORM_LIB_PATH "pnmrotate -noantialias $angle";
  119.     // End rotate
  120.  
  121.     /**
  122.      * Flip the image horizontally or vertically
  123.      *
  124.      * @param boolean $horizontal true if horizontal flip, vertical otherwise
  125.      */
  126.     function flip($horizontal
  127.     {
  128.         if($horizontal
  129.             $this->command[IMAGE_TRANSFORM_LIB_PATH "pnmflip -lr";
  130.         else
  131.             $this->command[IMAGE_TRANSFORM_LIB_PATH "pnmflip -tb";
  132.     }
  133.  
  134.     /**
  135.      * Adjust the image gamma
  136.      *
  137.      * @param float $outputgamma 
  138.      *
  139.      * @return none 
  140.      */
  141.     function gamma($outputgamma 1.0{
  142.         $this->command[13IMAGE_TRANSFORM_LIB_PATH "pnmgamma $outputgamma";
  143.     }
  144.  
  145.     /**
  146.      * adds text to an image
  147.      *
  148.      * @param   array   options     Array contains options
  149.      *              array(
  150.      *                   'text'          // The string to draw
  151.      *                   'x'             // Horizontal position
  152.      *                   'y'             // Vertical Position
  153.      *                   'Color'         // Font color
  154.      *                   'font'          // Font to be used
  155.      *                   'size'          // Size of the fonts in pixel
  156.      *                   'resize_first'  // Tell if the image has to be resized
  157.      *                                   // before drawing the text
  158.      *                    )
  159.      *
  160.      * @return none 
  161.      */
  162.     function addText($params)
  163.     {
  164.         $default_params array('text' => 'This is Text',
  165.                                 'x' => 10,
  166.                                 'y' => 20,
  167.                                 'color' => 'red',
  168.                                 'font' => 'Arial.ttf',
  169.                                 'size' => '12',
  170.                                 'angle' => 0,
  171.                                 'resize_first' => false);
  172.         // we ignore 'resize_first' since the more logical approach would be
  173.         // for the user to just call $this->_resize() _first_ ;)
  174.         extract(array_merge($default_params$params));
  175.         $this->command["ppmlabel -angle $angle -colour $color -size "
  176.                            ."$size -x $x -y ".$y+$size." -text \"$text\"";
  177.     // End addText
  178.  
  179.     function _postProcess($type$quality$save_type)
  180.     {
  181.         $type is_null($type|| $type==''$this->type $type;
  182.         $save_type is_null($save_type|| $save_type==''$this->type $save_type;
  183.         //echo "TYPE:". $this->type;
  184.                       . $type.'topnm '$this->image);
  185.         $arg '';
  186.         switch(strtolower($save_type)){
  187.             case 'gif':
  188.                 $this->command[IMAGE_TRANSFORM_LIB_PATH "ppmquant 256";
  189.                 $this->command[IMAGE_TRANSFORM_LIB_PATH "ppmto$save_type";
  190.                 break;
  191.             case 'jpg':
  192.             case 'jpeg':
  193.                 $arg "--quality=$quality";
  194.                 $this->command[IMAGE_TRANSFORM_LIB_PATH "ppmto$save_type $arg";
  195.                 break;
  196.             default:
  197.                 $this->command[IMAGE_TRANSFORM_LIB_PATH "pnmto$save_type $arg";
  198.                 break;
  199.         // switch
  200.         return implode('|'$this->command);
  201.     
  202.  
  203.     /**
  204.      * Save the image file
  205.      *
  206.      * @param $filename string the name of the file to write to
  207.      * @param string $type (jpeg,png...);
  208.      * @param int $quality 75
  209.      * @return none 
  210.      */
  211.     function save($filename$type=null$quality 85)
  212.     {
  213.         $cmd $this->_postProcess(''$quality$type">$filename";
  214.             
  215.         //if we have windows server
  216.         if(isset($_ENV['OS']&& eregi('window',$_ENV['OS']))
  217.             $cmd ereg_replace('/','\\',$cmd);
  218.         //echo $cmd."##";
  219.         $output system($cmd);
  220.         error_log('NETPBM: '.$cmd);
  221.         //error_log($output);
  222.         $this->command = array();
  223.     // End save
  224.  
  225.  
  226.     /**
  227.      * Display image without saving and lose changes
  228.      *
  229.      * @param string $type (jpeg,png...);
  230.      * @param int $quality 75
  231.      * @return none 
  232.      */
  233.     function display($type null$quality 75)
  234.     {
  235.         header('Content-type: image/' $type);
  236.         $cmd $this->_postProcess($type$quality);
  237.         
  238.         passthru($cmd);
  239.         $this->command = array();
  240.     }
  241.  
  242.     /**
  243.      * Destroy image handle
  244.      *
  245.      * @return none 
  246.      */
  247.     function free()
  248.     {
  249.         // there is no image handle here
  250.         return true;
  251.     }
  252.  
  253.  
  254. // End class NetPBM
  255. ?>

Documentation generated on Mon, 05 May 2008 16:21:35 +0400 by phpDocumentor 1.4.0